package com.aeoj.clientmanagement.controller;

import com.aeoj.clientmanagement.domain.problem.Problem;
import com.aeoj.clientmanagement.domain.problem.dto.ProblemSearchDTO;
import com.aeoj.clientmanagement.domain.problem.dto.ProblemSubmitDTO;
import com.aeoj.clientmanagement.domain.problem.vo.ProblemVO;
import com.aeoj.clientmanagement.domain.response.R;
import com.aeoj.clientmanagement.domain.user.SysUser;
import com.aeoj.clientmanagement.domain.user.vo.SysUserVO;
import com.aeoj.clientmanagement.enums.ResponseCodeEnum;
import com.aeoj.clientmanagement.enums.problem.ProblemSubmitLanguageEnum;
import com.aeoj.clientmanagement.exception.BusinessException;
import com.aeoj.clientmanagement.service.ProblemService;
import com.aeoj.clientmanagement.service.SysUserService;
import com.aeoj.clientmanagement.utils.user.UserUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ProblemController
 * author: ZSM
 * time: 2025-04-14 22:23
 */
@RestController
@RequestMapping("/client/problem")
public class ProblemController {
    @Resource
    private ProblemService problemService;
    @Resource
    private SysUserService sysUserService;

    /**
     * 获取题目列表
     * @param dto
     * @return
     */
    @GetMapping("list")
    public R list(ProblemSearchDTO dto) {
        Page<Problem> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("create_time");
        if (dto.getTitle() != null)
            queryWrapper.like("title", dto.getTitle());
        if (dto.getTags() != null) {
            for (String tag : dto.getTags()) {
                queryWrapper.like("tags", tag);
            }
        }
        if (dto.getDifficulty() != null)
            queryWrapper.eq("difficulty", dto.getDifficulty());

        IPage<Problem> problemIPage = problemService.page(page, queryWrapper);
        List<Problem> problemList = problemIPage.getRecords();
        List<ProblemVO> problemVOList = new ArrayList<>();
        problemList.forEach(x -> problemVOList.add(ProblemVO.toProblemVO(x)));

        IPage<ProblemVO> res = new Page<>();
        res.setRecords(problemVOList);
        res.setTotal(problemIPage.getTotal());
        res.setSize(problemIPage.getSize());
        res.setCurrent(problemIPage.getCurrent());

        return R.ok(res);
    }

    /**
     * 获取题目详情
     * @param id
     * @param request
     * @return
     */
    @GetMapping("get")
    public R get(Integer id, HttpServletRequest request) {
        UserUtils.getLoginUser(request);
        if (id == null || id <= 0)
            throw new BusinessException(ResponseCodeEnum.BAD_REQUEST);
        Problem problem = problemService.getById(id);
        ProblemVO problemVO = ProblemVO.toProblemVO(problem);
        SysUser user = sysUserService.getById(problem.getCreateBy());
        if (user != null)
            problemVO.setAuthor(user.getNickName());
        return R.ok(problemVO);
    }

    /**
     * 提交题目
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("submit")
    public R submit(@RequestBody ProblemSubmitDTO dto, HttpServletRequest request) {
        SysUserVO loginUser = UserUtils.getLoginUser(request);

        if (dto == null || dto.getProblemId() == null || dto.getProblemId() <= 0)
            throw new BusinessException(ResponseCodeEnum.BAD_REQUEST);

        Long problemRecordId = problemService.submit(dto, loginUser);
        return R.ok("提交成功", problemRecordId);
    }

    @GetMapping("language")
    public R listLanguage() {
        Map<String, String> languageMap = new HashMap<>();
        ProblemSubmitLanguageEnum[] values = ProblemSubmitLanguageEnum.values();
        for (ProblemSubmitLanguageEnum item : values) {
            languageMap.put(item.getCode(), item.getName());
        }
        return R.ok(languageMap);
    }
}
